function initPage(pageNo,totalCount,arr){
    // 获取分页器实例对象
    var myPager = $('#myPager').data('zui.pager');
    //设置当前页码为 4，并同时设置记录总数为 100， 每页记录数目为 30
    myPager.set(pageNo, totalCount, 5);
    renderSubscribeList(arr);
}
$('#myPager').pager({
    onPageChange: function(state, oldState) {
        if (state.page !== oldState.page) {
            // console.log('页码从', oldState.page, '变更为', state.page);
            queryBtn(state.page);

        }
    }
});
function renderSubscribeList(arr){
    let str = '';
    for (let i = 0; i < arr.length; i++) {
        let a  = arr[i];
        var systime = a.finishTime == 0 ? '暂无数据':a.finishTime;
        if (a.subStatus == '已预约'){
            str += `<tr>
                    <td>${a.subTime}</td>
                    <td>${a.consName}</td>
                    <td>${a.fieldName}</td>
                    <td>${a.username}</td>
                    <td>${systime}</td>
                    <td>${a.subStatus}</td>
                    <td  style="text-align: left">
                        <button onclick="subInfo(id)"
                        type="button" data-toggle="modal" data-target="#myModal2" 
                        id="updateBtn"  
                        class="btn btn-info updateBtn btn-primary">
                        查看预约</button>
                        <button onclick="banBtn(${a.id})" 
                        data-toggle="modal" data-target="#myModal3" 
                        id="delBtn" 
                        class="delBtn btn btn-danger btn-primary" >
                        终止预约</button>
                    </td>
                </tr>`;
        }else {
            str += `<tr>
                    <td>${a.subTime}</td>
                    <td>${a.consName}</td>
                    <td>${a.fieldName}</td>
                    <td>${a.username}</td>
                    <td>${systime}</td>
                    <td>${a.subStatus}</td>
                    <td style="text-align: left">
                        <button onclick="subInfo(${a.id})" data-toggle="modal"
                         data-target="#myModal2" id="delBtn" 
                         class="btn btn-info updateBtn btn-primary" >查看详情</button>
                    </td>
                </tr>`;
        }

    }
    $('#tbody')[0].innerHTML = str;
}
function subInfo(id){

    console.log(id);
    $.ajax({
        url:config.url + "/order/subscribeInfoForOne",
        dataType:'json',
        xhrFields: {
            withCredentials: true
        },
        data:{
            id:id
        },
        crossDomain: true,
        type:'GET',
        success:function(resp) {
            var consInfo = resp.data;
            var str = '';
            str+=
                `
                    <ul class="list-group">
                            <li class="list-group-item">咨询人:&nbsp;&nbsp;${consInfo.username}</li>
                            <li class="list-group-item">领域:  ${consInfo.fieldName}</li>
                            <li class="list-group-item">预约时间:&nbsp;&nbsp;${consInfo.subTime}</li>
                            <li class="list-group-item">预约状态:&nbsp;&nbsp;${consInfo.subStatus}</li>
                            <li class="list-group-item">费用:&nbsp;&nbsp;${consInfo.cost}</li>
                            <li class="list-group-item">咨询师:&nbsp;&nbsp;${consInfo.consName}</li>
                            <li class="list-group-item">问题描述:&nbsp;&nbsp;${consInfo.questionInfo}</li>
                            <li class="list-group-item">诊断答复：${consInfo.answer == null || consInfo.answer == ''? '暂无':consInfo.answer}</li>
                            <li class="list-group-item">答复时间:&nbsp;&nbsp;${consInfo.answerTime == null || consInfo.answerTime == ''? '暂无':consInfo.answerTime}</li>
                            <li class="list-group-item">评价内容:&nbsp;&nbsp;${consInfo.evaluate == null || consInfo.evaluate == ''? '暂无评价':consInfo.evaluate}</li>
                        </ul>
                `
            $('#sub-info-div')[0].innerHTML = str;
        },
        error:function() {
        }
    });
}
var subId;
function banBtn(id){
    subId = id;
    console.log(subId);
}
function submitBanForm(){
    $.ajax({
        url: config.url + "/sub/ban",
        dataType: 'json',
        xhrFields: {
            withCredentials: true
        },
        data: {
            id: subId
        },
        crossDomain: true,
        type: 'GET',
        success: function (resp) {
            alert(resp.msg);
            location.reload();
        },
        error: function () {
        }
    });
}
queryBtn(1);
function queryBtn(i){
    let consName = $("#cons-input").val();
    let username = $("#user-input").val();
    let stime = $("#date-input1").val();
    let etime = $("#date-input2").val();
    if (consName == null || consName == ""){
        consName == "";
    }
    if (username == null || username == ""){
        username == "";
    }
    console.log(consName == "")
    console.log(username == "")
    console.log(stime)
    console.log(etime)
    $.ajax({
        url:config.url + "/sub/queryForCondition",
        dataType:'json',
        xhrFields: {
            withCredentials: true
        },
        data:{
            username:username,
            consName:consName,
            stime:stime,
            etime:etime,
            pageNo:i
        },
        crossDomain: true,
        type:'POST',
        success:function(resp) {
            var totalCount = resp.data.totalCount;
            renderSubscribeList(resp.data.list);
            initPage(resp.pageNo,totalCount,resp.data.list);
        },
        error:function() {
        }
    });
}
